<html >
<head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
    <title>注册</title>
    <link href="css/bootstrap/bootstrap.min.css" title="" rel="stylesheet" />
    <link href="css/register.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div style="height:1px;"></div>
<div class="login">
    <header>
        <h1>注册</h1>
    </header>
    <div class="sr">
        <form method="post" action="" class="form-horizontal" id="registerForm">
            <div class="name">
                <input type="text" name="username" placeholder="请输入用户名" class="name_inp" id="username">
            </div>
            <div class="name">
                <input type="password" name="password" placeholder="请输入密码" class="name_inp" id="password">
            </div>
            <div class="name">
                <input type="password" name="confirmPassword" placeholder="请再次输入密码" class="name_inp" id="confirmPassword">
            </div>
            <div class="name">
                <!--<input type="text" name="college" placeholder="请输入学院" class="name_inp" id="college">-->
                <select class="form-control" name="school" id="school">
                    <option value="1">国服学院</option>
                    <option value="2">人文学院</option>
                    <option value="3">经济与管理学院</option>
                    <option value="4">教育学院</option>
                    <option value="5">沈钧儒法学院</option>
                    <option value="6">外国语学院</option>
                </select>
            </div>
            <div class="name">
                <select name="usertype" id="usertype" class="form-control">
                    <option value="1">教师</option>
                    <option value="2">学生</option>
                    <option value="3">管理员</option>
                </select>
            </div>
            <div class="name">
                <input type="number" name="telephone" placeholder="请输入手机号码" class="name_inp" id="telephone">
            </div>

            <div class="dl" data-toggle="buttons-radio">
                <button type="submit" class="btn btn-primary" >注册</button>
                <button type="reset" class="btn btn-primary">重置</button>
            </div>
        </form>
    </div>
</div>
<script src="js/jquery-2.1.1.min.js"></script>
<script>
    $(function () {
        $('#registerForm').submit(function (event) {
            event.preventDefault();
            //获取用户的输入
            var username = $('#username').val();
            var password = $('#password').val();
            var confirmPassword = $('#confirmPassword').val();
            var school = $('#school').val();
            var usertype = $('#usertype').val();
            var telephone = $('#telephone').val();
            //判断用户输入是否存在空值，或者输入信息是否正确
            if (username.length == 0  || password.length == 0 || confirmPassword.length == 0 || telephone.length == 0){
                alert("请填入必要的信息字段！！！");
                window.location.href="register.html";
            }else{
                if (password != confirmPassword){
                    alert("两次输入的密码不同！！！");
                    window.location.href="register.html";
                }else{
                    // console.log(school);
                    var user = JSON.stringify({"schoolid":school,"username":username,"password":password,"mobilephone":telephone,"usertype":usertype,"state":1});

                    $.ajax({
                        type:"post",
                        url:"/user/addOne",
                        data:user,
                        contentType:"application/json",
                        dataType:"json",
                        success:function (data) {
                            console.log(data);
                            if (data.msg=='success'){
                                alert("注册成功");
                                window.location.href="login.html";
                            }else{
                                alert(data.msg);
                                window.location.href="register.html";
                            }
                        }
                    })
                }
            }
        })
    })
</script>
</body>
</html>